package com.epoint.mobileoa.tools.controls.tree.codeitemtree;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Element;

import android.os.Message;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.datanalysis.XMLHelp;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.http.HttpHelp;
import com.epoint.mobileoa.model.CodeItemsModel;
import com.epoint.mobileoa.tools.controls.tree.Node;
import com.epoint.mobileoa.tools.controls.tree.TreeAdapter;

/**
 * @author liyc
 * @time 2012-7-7 上午10:40:32
 * @annotation 
 */
public class InitCodeItemTreeTask extends Task {

	private boolean unleafchoose;
	private String CodeName;
	private String style;

	public InitCodeItemTreeTask(SuperView con, HashMap<String, Object> param) {
		super(con, param);
	}

	@Override
	public void exectue(Message mess) throws Exception {
		
		String nodeKey = getParam().get("nodeKey").toString();
		int type = (Integer) getParam().get("type");
		byte[] odata = DBHelp.getRes(getCon(), nodeKey);
		Node root;
		if(odata!=null){
			ByteArrayInputStream bais = new ByteArrayInputStream(odata);
			ObjectInputStream ois = new ObjectInputStream(bais);
			root = (Node) ois.readObject();
		}
		else{
			
			style = getParam().get("style")==null?null:getParam().get("style").toString();
			unleafchoose = (Boolean) getParam().get("unleafchoose");
			String rootitemvalue = getParam().get("rootitemvalue").toString();
			String backgetDataMethod = getParam().get("backgetDataMethod").toString();
			
			CodeName = getParam().get("CodeName").toString();
			
			String url = generateURL(backgetDataMethod,CodeName);
			String bs = HttpHelp.getHttpBack(url);
			List<CodeItemsModel> list = XmlAnalysis(bs);
			
			
			DBHelp.saveCode_Items(getCon(),list);
			

			//创建根节点
			root = new Node("列表",null,null);
			root.setIcon(R.drawable.icon_department);//设置图标
			setNode(rootitemvalue, root);
			root.setCheckBox(false);

			ByteArrayOutputStream fos = new ByteArrayOutputStream();
			ObjectOutputStream oos;
			oos = new ObjectOutputStream(fos);
			oos.writeObject(root);
			byte[] b = fos.toByteArray();
			DBHelp.saveResData(getCon(), nodeKey, b);
		}

		TreeAdapter ta = new TreeAdapter(getCon(),root,type);
		ta.setCheckBox(true);
		ta.setExpandedCollapsedIcon(R.drawable.tree_ex, R.drawable.tree_ec);
		ta.setExpandLevel(1);

		setMessageToRefreshUI(mess, getCon(), ta);
	}

	public void setNode(String parentitemvalue,Node node)
	{

		List<CodeItemsModel> list = getData(parentitemvalue);

		for(int i=0;i<list.size();i++){

			Node n = new Node(list.get(i).ItemText,list.get(i).ItemValue,list.get(i).CodeName);
			n.setParent(node);

			n.setIcon(R.drawable.icon_police);
			n.setCheckBox(true);

			if(n.getParent()!=null){
				n.getParent().setIcon(R.drawable.icon_department);
				n.getParent().setCheckBox(true);
			}

			if(unleafchoose){
				if(n.getChildren()!=null){
					n.getParent().setIcon(R.drawable.icon_department);
					n.getParent().setCheckBox(false);
				}
			}
			n.setExpanded(false);
			node.add(n);
			setNode(list.get(i).ItemValue,n);
		}
	}

	private List<CodeItemsModel> getData(String parentitemvalue) {
		List<CodeItemsModel> list = DBHelp.getChildCodeItems(getCon(), CodeName, parentitemvalue,style);
		return list;
	}

	public static List<CodeItemsModel> XmlAnalysis(String xml){
		List focs = XMLHelp.getRoot(xml).elements();
		List<CodeItemsModel> plist = new ArrayList<CodeItemsModel>();
		for (int i = 0; i < focs.size(); i++) {
			CodeItemsModel mt = new CodeItemsModel();
			Element foc = (Element) focs.get(i);
			if (foc.element("ItemValue") != null)
				mt.ItemValue = foc.elementText("ItemValue");
			if (foc.element("ItemText") != null)
				mt.ItemText = foc.elementText("ItemText");
			if (foc.element("ItemID") != null)
				mt.ItemID = foc.elementText("ItemID");
			if (foc.element("CodeID") != null)
				mt.CodeID = foc.elementText("CodeID");
			if (foc.element("CodeName") != null)
				mt.CodeName = foc.elementText("CodeName");
			plist.add(mt);
		}
		return plist;
	}

}
